﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace nr.Tombola.Controls
{
    public partial class TombolaNumber : UserControl
    {
        private int number;
        private bool extract;

        public int Number
        {
            get { return number; }
            set
            {
                if (value != number)
                {
                    number = value;
                    Invalidate();
                }
            }
        }

        public bool Extract
        {
            get { return extract; }
            set
            {
                if (extract != value)
                {
                    extract = value;
                    Invalidate();
                }
            }
        }

        public TombolaNumber()
        {
            InitializeComponent();
            Number = 88;
            Extract = false;
        }

        private int AvailableWidth { get { return Size.Width - Margin.Left - Margin.Right - Padding.Left - Padding.Right; } }

        private int AvailableHeight { get { return Size.Height - Margin.Top - Margin.Bottom - Padding.Top - Padding.Bottom; } }

        private Font CalculateFont(Graphics g)
        {
            Font result = Font;

            SizeF fontSize = SizeF.Empty;
            int size = 101;
            do
            {
                result = new Font(result.FontFamily, --size);
                fontSize = g.MeasureString("88", result);
            } while (fontSize.Width > AvailableWidth || fontSize.Height > AvailableHeight);

            return result;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.Clear(this.BackColor);
            Pen borderPen = Extract ? new Pen(Brushes.Maroon, 3) : new Pen(Brushes.Black, 1);
            Brush innerBrush = Extract ? Brushes.PaleVioletRed : Brushes.Gray;
            Brush numberBrush = Extract ? Brushes.White : Brushes.Black;
            Rectangle rect = new Rectangle(Margin.Left, Margin.Top, AvailableWidth, AvailableHeight);
            e.Graphics.FillEllipse(innerBrush, rect);
            e.Graphics.DrawEllipse(borderPen, rect);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(Number.ToString(), CalculateFont(e.Graphics), numberBrush, rect, sf);
        }

        private void TombolaNumber_Paint(object sender, PaintEventArgs e)
        {
        }
    }
}
